package cn.lbd.api.lambda;

import org.junit.jupiter.api.Test;

import java.util.*;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;

public class Demo1 {

    private List<Employee> lists = Arrays.asList(
            new Employee(12, "张三", 9999.99),
            new Employee(22, "李四", 8888.88),
            new Employee(32, "王五", 6999.99),
            new Employee(42, "胡时飞", 6000.11),
            new Employee(52, "贾科斯", 4999.11),
            new Employee(62, "傻子", 3444.44)
    );

    //寻找年龄大于30的员工
    public List<Employee> findByAge(List<Employee> lists) {
        List<Employee> emps = new ArrayList<>();
        for (Employee list : lists) {
            if (list.getAge() > 30) {
                emps.add(list);
            }
        }
        System.out.println("蔡圣沅大王又来了");
        return emps;
    }

    //寻找工资小于6000的员工
    public List<Employee> findBySalary(List<Employee> lists) {
        List<Employee> emps = new ArrayList<>();
        for (Employee list : lists) {
            if (list.getSalary() < 6000) {
                emps.add(list);
            }
        }
        return emps;
    }


    @Test
    public void test1() {
        List<Employee> emps = findByAge(lists);
        emps.forEach(System.out::println);
    }

    @Test
    public void test2() {
        List<Employee> emps = findBySalary(lists);
        emps.forEach(System.out::println);
    }
    //上面的写法有大量重复代码，实际上要修改的东西就一行


    /**
     * 策略模式
     *
     * @param lists
     * @param mp    根据不同需求实例不同业务，达到条件就返回true
     * @return
     */
    public List<Employee> filterEmp(List<Employee> lists, MyPredicate<Employee> mp) {
        List<Employee> records = new ArrayList<>();
        for (Employee list : lists) {
            if (mp.test(list)) {
                records.add(list);
            }
        }
        return records;
    }

    //外部实现接口，内部传实现类
    @Test
    public void test3() {
        MyPredicate<Employee> myPredicate = new FindBySalary();
        filterEmp(lists, myPredicate).forEach(System.out::println);
    }

    //匿名内部类
    @Test
    public void test4() {
        filterEmp(lists, new MyPredicate<Employee>() {
            @Override
            public boolean test(Employee employee) {
                return employee.getAge() > 30 && employee.getAge() < 60;
            }
        }).forEach(System.out::println);
    }

    //lambda
    @Test
    public void test5() {
        filterEmp(lists, (e) -> e.getAge() < 23).forEach(System.out::println);
    }

    //方法引用
    @Test
    public void test05() {
        filterEmp(lists, (e) -> e.getAge() < 23).forEach(System.out::println);
    }

    //stream流
    @Test
    public void test6() {
        lists.stream()
                .filter((e) -> e.getSalary() >= 6000)
                .limit(2)
                .forEach(System.out::println);

        System.out.println("----------------------------");
        lists.stream()
                .filter((employee -> employee.getSalary() > 6000))
                .map(Employee::getName)
                .forEach(System.out::println);
    }

    @Test
    public void test7() {
        Integer num = operation(100, (x) -> x * x);
        System.out.println(num);

        Integer num1 = operation(300, (y) -> y - 200);
        System.out.println(num1);
    }

    public Integer operation(Integer num, MyFun<Integer> mf) {
        return mf.getValue(num);
    }




}
